// 判断arr是否为一个数组，返回一个bool值
export const isArray = function(arr) {
	return Object.prototype.toString.call(arr) === '[object Array]';
}

// 深度克隆
export const deepClone = function(obj) {
	// 对常见的“非”值，直接返回原来值
	if ([null, undefined, NaN, false].includes(obj)) return obj;
	if (typeof obj !== "object" && typeof obj !== 'function') {
		//原始类型直接返回
		return obj;
	}
	var o = isArray(obj) ? [] : {};
	for (let i in obj) {
		if (obj.hasOwnProperty(i)) {
			o[i] = typeof obj[i] === "object" ? deepClone(obj[i]) : obj[i];
		}
	}
	return o;
}



// JS对象深度合并
export const deepMerge = function(target = {}, source = {}) {
	target = deepClone(target);
	if (typeof target !== 'object' || typeof source !== 'object') return false;
	for (var prop in source) {
		if (!source.hasOwnProperty(prop)) continue;
		if (prop in target) {
			if (typeof target[prop] !== 'object') {
				target[prop] = source[prop];
			} else {
				if (typeof source[prop] !== 'object') {
					target[prop] = source[prop];
				} else {
					if (target[prop].concat && source[prop].concat) {
						target[prop] = target[prop].concat(source[prop]);
					} else {
						target[prop] = deepMerge(target[prop], source[prop]);
					}
				}
			}
		} else {
			target[prop] = source[prop];
		}
	}
	return target;
}


function padLeftZero(str) {
	return ('00' + str).substr(str.length);
}

//日期格式化
export const handleDate = function(date, fmt = 'yyyy-MM-dd') {
	if (!date) return '';
	if (typeof date === 'string') {
		// 苹果不支持 'new Date(2020-12-30)' 
		date = new Date(date.replace(/-/g, '/'));
	}
	if (/(y+)/.test(fmt)) {
		fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
	}
	let o = {
		'M+': date.getMonth() + 1,
		'd+': date.getDate(),
		'h+': date.getHours(),
		'm+': date.getMinutes(),
		's+': date.getSeconds()
	};
	for (let k in o) {
		if (new RegExp(`(${k})`).test(fmt)) {
			let str = o[k] + '';
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
		}
	}
	return fmt;
}

//处理数量
export const handleCount = function(count, unit) {
	if(count<unit){
		return count;
	}
	switch (unit) {
		case 1000:
		 count = Math.round(count/100)/10 + 'K';
			break;
		case 10000:
		 count = Math.round(count/1000)/10 + 'W';
			break;
		default:
			break;
	}
	return count;
}


/**
 * 缓存请求结果
 * @param {String} url  请求接口地址
 * @param {Object} params  请求参数
 * @param {Function} callback
 * @param {*} expirationxt 缓存有效期 默认半小时 单位分钟
 */
export const getCacheData = async (
  url = '',
  params = {},
  callback = async () => {},
  expirationxt = 30
) => {
  const key = url + JSON.stringify(params);
  const value = uni.getStorageSync(key);
  const now = +new Date();
  let result = (value && JSON.parse(value || '')) || {};
  // 没存储 或 已过期
  if (!value || result.expirationxt < now - expirationxt * 60 * 1000) {
    // 获取内容并设置缓存
    result = await callback();
	// 请求成功 则缓存
	if (result.resultCode === "200"){
		uni.setStorageSync(
		  key,
		  JSON.stringify({
			data: result.data,
			resultCode: resutl.resultCode,
			expirationxt: now,
		  })
		);
	}
  }
  // 返回数据
  return result;
};